<?php

namespace app\api\middleware;
use think\exception\ValidateException;


class JwtAuth
{

    public function handle($request, \Closure $next)
    {
		$token = $request->header('Authorization');
		if(!$token){
			throw new ValidateException('token不能为空');
		}
		// 检查Token格式，以.隔开，分成三段
		if(count(explode('.',$token)) <> 3){
			throw new ValidateException('token格式错误');
		}
		// 验证Token
        $verifyResult = checkToken($token);
        if($verifyResult['status']){
            $request->userInfo = $verifyResult['data'];
            return $next($request);
        } else {
            return json(['status'=>102,'msg'=>$verifyResult['info']]);
        }
    }
}
